<template>
<div class="base-container">
  <t-tabs @change="changeTab" placement="left"  size="medium" v-model="stepValue">
    <t-tab-panel v-if="openLX" style="overflow-x: scroll"  value="1" label="项目申报">
      <project-apply :closeSubmit="true">
      </project-apply>
    </t-tab-panel>
    <t-tab-panel style="overflow-x: scroll"  value="2" label="大纲编制">
      <project-outline v-if="openDG"  :closeSubmit="true"></project-outline>
    </t-tab-panel>
    <t-tab-panel style="overflow-x: scroll" value="3" label="任务书签订">
      <project-contract v-if="openRWS"  :closeSubmit="true"></project-contract>
    </t-tab-panel>
    <t-tab-panel style="overflow-x: scroll"  value="4" label="验收申请">
      <acceptance-apply v-if="openYS" :closeSubmit="true"></acceptance-apply>
    </t-tab-panel>
  </t-tabs>
</div>
</template>

<script>
import projectApply from "./pm/project-apply.vue";
import projectOutline from "./outline/project-outline.vue";
import projectContract from "./contract/project-contract.vue";
import acceptanceApply from "./acceptance/acceptance-apply.vue";
import {getFiles} from "@/api/common";


export default {
  name: "all-apply",
  components:{
    projectApply,
    projectOutline,
    projectContract,
    acceptanceApply
  },
  data(){
    return {
      stepValue: '',
      taskId: null,
      openDG: false,
      openRWS: false,
      openYS: false,
      openLX: false,
    }
  },
  async created() {
    const taskId = this.$route.params && this.$route.params.taskId;
    if (!taskId) {
      this.$message.error("任务id丢失返回项目");
      this.$router.push("/kjxm/pm/project-my");
    }
    this.taskId = taskId;
    this.stepValue = sessionStorage.getItem(`${this.taskId}-all-apply-active`) || '1'
    this.projectInit();
    this.outlineInit();
    this.contractInit();
    this.acceptanceInit();
  },
  methods:{
    projectInit(){
      sessionStorage.removeItem(`${this.taskId}-activeForm`);
      sessionStorage.setItem(`${this.taskId}-projectId`, this.taskId);
      this.$api.kjxm.project.getAll(this.taskId).then(response => {
        const activeFormList = [];
        if (response.data.projectBackground && response.data.projectBackground.id) {
          sessionStorage.setItem(`${this.taskId}-backgroundId`, response.data.projectBackground.id)
        } else {
          activeFormList.push(1);
        }
        if (response.data.projectApproach && response.data.projectApproach.id) {
          sessionStorage.setItem(`${this.taskId}-approachId`, response.data.projectApproach.id)
        } else {
          activeFormList.push(2);
        }
        if (response.data.projectBasicstudy && response.data.projectBasicstudy.id) {
          sessionStorage.setItem(`${this.taskId}-basicstudyId`, response.data.projectBasicstudy.id)
        } else {
          activeFormList.push(3);
        }
        if (response.data.projectCompanyList && response.data.projectCompanyList.length > 0) {
          sessionStorage.setItem(`${this.taskId}-companyId`, response.data.projectCompanyList.length)
        } else {
          activeFormList.push(4);
        }
        if (response.data.projectMembers && response.data.projectMembers.length > 0) {
          sessionStorage.setItem(`${this.taskId}-memberId`, response.data.projectMembers.length)
        } else {
          activeFormList.push(5);
        }
        if (response.data.projectBudget && response.data.projectBudget.id) {
          sessionStorage.setItem(`${this.taskId}-budgetId`, response.data.projectBudget.id)
        } else {
          activeFormList.push(6);
        }
        // 查询附件
        // getFiles(this.taskId).then((res) => {
        //   if (res.data.length <= 0) {
        //     activeFormList.push(7);
        //   } else {
        //     sessionStorage.setItem(`${this.taskId}-fileId`, res.data.length)
        //   }
        // });
        if (activeFormList.length <= 0) {
          sessionStorage.setItem(`${this.taskId}-activeForm`, 7); // 跳转到最后一步
        } else {
          sessionStorage.setItem(`${this.taskId}-activeForm`, activeFormList[0]); // 跳转到第一个不满足
        }

        this.openLX = true
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    outlineInit(){
      sessionStorage.removeItem(`${this.taskId}-outline-activeForm`)
      this.$api.kjxm.outline.getAll(this.taskId).then(response=> {
        if(response.data?.id){
          const activeFormList = [];
          sessionStorage.setItem(`${this.taskId}-outlineId`, response.data.id)
          sessionStorage.removeItem(`${this.taskId}-outline-outlineId`);
          sessionStorage.removeItem(`${this.taskId}-outline-hold-outlineId`);
          if(response.data.isSubmit==="1"){
            sessionStorage.setItem(`${this.taskId}-outline-outlineId`, response.data.id)
          }else {
            sessionStorage.setItem(`${this.taskId}-outline-hold-outlineId`, response.data.id)
            activeFormList.push(0);
          }
          if (response.data?.projectOutlinePreWork && response.data?.projectOutlinePreWork?.id) {
            sessionStorage.removeItem(`${this.taskId}-outline-basicstudyId`);
            sessionStorage.removeItem(`${this.taskId}-outline-hold-basicstudyId`);
            if (response.data?.projectOutlinePreWork.isSubmit === '1') {
              sessionStorage.setItem(`${this.taskId}-outline-basicstudyId`, response.data.projectOutlinePreWork.id)
            } else {
              sessionStorage.setItem(`${this.taskId}-outline-hold-basicstudyId`, response.data.projectOutlinePreWork.id)
              activeFormList.push(1);
            }
          } else {
            activeFormList.push(1);
          }
          if (response.data?.projectOutlineNecessity && response.data?.projectOutlineNecessity?.id) {
            sessionStorage.removeItem(`${this.taskId}-outline-backgroundId`);
            sessionStorage.removeItem(`${this.taskId}-outline-hold-backgroundId`);
            if (response.data?.projectOutlineNecessity.isSubmit === '1') {
              sessionStorage.setItem(`${this.taskId}-outline-backgroundId`, response.data.projectOutlineNecessity.id)
            } else {
              sessionStorage.setItem(`${this.taskId}-outline-hold-backgroundId`, response.data.projectOutlineNecessity.id)
              activeFormList.push(2);
            }
          } else {
            activeFormList.push(2);
          }
          if (response.data?.projectOutlineImplPlan && response.data?.projectOutlineImplPlan?.id) {
            sessionStorage.removeItem(`${this.taskId}-outline-approachId`);
            sessionStorage.removeItem(`${this.taskId}-outline-hold-approachId`);
            if (response.data?.projectOutlineImplPlan.isSubmit === '1') {
              sessionStorage.setItem(`${this.taskId}-outline-approachId`, response.data.projectOutlineImplPlan.id)
            } else {
              sessionStorage.setItem(`${this.taskId}-outline-hold-approachId`, response.data.projectOutlineImplPlan.id)
              activeFormList.push(3);
            }
          } else {
            activeFormList.push(3);
          }
          if (response.data?.projectCompanyList && response.data?.projectCompanyList.length > 0) {
            sessionStorage.removeItem(`${this.taskId}-outline-companyId`);
            sessionStorage.removeItem(`${this.taskId}-outline-hold-companyId`);
            // 如果列表存在 isSubmit = '0' 则为暂存
            const n = response.data?.projectCompanyList.filter(val => val.isSubmit === '0')
            if (n.length > 0) {
              activeFormList.push(4);
              sessionStorage.setItem(`${this.taskId}-outline-hold-companyId`, response.data?.projectCompanyList.length)
            } else {
              sessionStorage.setItem(`${this.taskId}-outline-companyId`, response.data?.projectCompanyList.length)
            }
          } else {
            activeFormList.push(4);
          }
          if (response.data?.projectMemberList && response.data?.projectMemberList.length > 0) {
            sessionStorage.removeItem(`${this.taskId}-outline-memberId`);
            sessionStorage.removeItem(`${this.taskId}-outline-hold-memberId`);
            const n = response.data?.projectMemberList.filter(val => val.isSubmit === '0')
            if (n.length > 0) {
              activeFormList.push(5);
              sessionStorage.setItem(`${this.taskId}-outline-hold-memberId`, response.data?.projectMemberList.length)
            } else {
              sessionStorage.setItem(`${this.taskId}-outline-memberId`, response.data?.projectMemberList.length)
            }
          } else {
            activeFormList.push(5);
          }
          if (response.data?.projectBudget && response.data?.projectBudget?.id) {
            if (response.data?.projectBudget.isSubmit === '1') {
              sessionStorage.setItem(`${this.taskId}-outline-budgetId`, response.data.projectBudget.id)
            } else {
              sessionStorage.setItem(`${this.taskId}-outline-hold-budgetId`, response.data.projectBudget.id)
              activeFormList.push(6);
            }
          } else {
            activeFormList.push(6);
          }
          if (response.data?.projectOutlineOther && response.data?.projectOutlineOther?.id) {
            sessionStorage.removeItem(`${this.taskId}-outline-otherId`);
            sessionStorage.removeItem(`${this.taskId}-outline-hold-otherId`);
            if (response.data?.projectOutlineOther.isSubmit === '1') {
              sessionStorage.setItem(`${this.taskId}-outline-otherId`, response.data.projectOutlineOther.id)
            } else {
              sessionStorage.setItem(`${this.taskId}-outline-hold-otherId`, response.data.projectOutlineOther.id)
              activeFormList.push(7);
            }
          } else {
            activeFormList.push(7);
          }
          if (activeFormList.length <= 0) {
            sessionStorage.setItem(`${this.taskId}-outline-activeForm`, 7); // 跳转到最后一步
          } else {
            sessionStorage.setItem(`${this.taskId}-outline-activeForm`, activeFormList[0]); // 跳转到第一个不满足
          }
          this.openDG = true
        }else {
          this.openDG = true
        }
      }).catch((e) => {
        this.$message.error(e.toString());
      });
      // this.$api.kjxm.outline.getByProject(this.taskId).then(response => {
      //   if(response.data){
      //     sessionStorage.setItem(`${this.taskId}-outlineId`, response.data.id)
      //     sessionStorage.setItem(`${this.taskId}-outline-hold-outlineId`, response.data.id)
      //   }
      //   this.openDG = true
      // }).catch((e) => {
      //   this.$message.error(e.toString());
      // });
    },
    contractInit(){
      sessionStorage.removeItem(`${this.taskId}-contract-activeForm`)
      this.$api.kjxm.contract.getAll(this.taskId).then(response=>{
        if(response.data?.id){
          sessionStorage.setItem(`${this.taskId}-contractId`, response.data?.id)
          sessionStorage.removeItem(`${this.taskId}-contract-contractId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-contractId`);
          const activeFormList = [];
          if(response.data.isSubmit==="1"){
            sessionStorage.setItem(`${this.taskId}-contract-contractId`, response.data.id)
          }else {
            sessionStorage.setItem(`${this.taskId}-contract-hold-contractId`, response.data.id)
            activeFormList.push(0);
          }
          // sessionStorage.setItem(`${this.taskId}-contract-contractId`, response.data?.id)
          sessionStorage.removeItem(`${this.taskId}-contract-purposeContentId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-purposeContentId`);
          if (response.data?.contractPurposeContent && response.data?.contractPurposeContent?.id) {
            if(response.data?.contractPurposeContent.isSubmit==='1'){
              sessionStorage.setItem(`${this.taskId}-contract-purposeContentId`, response.data.contractPurposeContent.id)
            }else {
              sessionStorage.setItem(`${this.taskId}-contract-hold-purposeContentId`, response.data.contractPurposeContent.id)
              activeFormList.push(1);
            }
          } else {
            activeFormList.push(1);
          }
          sessionStorage.removeItem(`${this.taskId}-contract-assessIndexId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-assessIndexId`);
          if (response.data?.contractAssessIndex && response.data?.contractAssessIndex?.id) {
            if(response.data?.contractAssessIndex.isSubmit==='1'){
              sessionStorage.setItem(`${this.taskId}-contract-assessIndexId`, response.data.contractAssessIndex.id)
            }else {
              sessionStorage.setItem(`${this.taskId}-contract-hold-assessIndexId`, response.data.contractAssessIndex.id)
              activeFormList.push(2);
            }
          } else {
            activeFormList.push(2);
          }
          sessionStorage.removeItem(`${this.taskId}-contract-researchPlanId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-researchPlanId`);
          if (response.data?.contractResearchPlans && response.data?.contractResearchPlans.length>0) {
            // 如果列表存在 isSubmit = '0' 则为暂存
            const n = response.data?.contractResearchPlans.filter(val=>val.isSubmit === '0')
            if(n.length>0){
              activeFormList.push(3);
              sessionStorage.setItem(`${this.taskId}-contract-hold-researchPlanId`, response.data?.contractResearchPlans.length)
            }else {
              sessionStorage.setItem(`${this.taskId}-contract-researchPlanId`, response.data?.contractResearchPlans.length)
            }
          } else {
            activeFormList.push(3);
          }
          sessionStorage.removeItem(`${this.taskId}-contract-resultsId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-resultsId`);
          if (response.data?.contractResults && response.data?.contractResults?.id) {
            if(response.data?.contractResults.isSubmit==='1'){
              sessionStorage.setItem(`${this.taskId}-contract-resultsId`, response.data.contractResults.id)
            }else {
              sessionStorage.setItem(`${this.taskId}-contract-hold-resultsId`, response.data.contractResults.id)
              activeFormList.push(4);
            }
          } else {
            activeFormList.push(4);
          }
          sessionStorage.removeItem(`${this.taskId}-contract-companyId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-companyId`);
          if (response.data?.projectCompanyList && response.data?.projectCompanyList.length>0) {
            // 如果列表存在 isSubmit = '0' 则为暂存
            const n = response.data?.projectCompanyList.filter(val=>val.isSubmit === '0')
            if(n.length>0){
              activeFormList.push(5);
              sessionStorage.setItem(`${this.taskId}-contract-hold-companyId`, response.data?.projectCompanyList.length)
            }else {
              sessionStorage.setItem(`${this.taskId}-contract-companyId`, response.data?.projectCompanyList.length)
            }
          } else {
            activeFormList.push(5);
          }
          sessionStorage.removeItem(`${this.taskId}-contract-memberId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-memberId`);
          if (response.data?.projectMemberList && response.data?.projectMemberList.length>0) {
            const n = response.data?.projectMemberList.filter(val=>val.isSubmit === '0')
            if(n.length>0){
              activeFormList.push(6);
              sessionStorage.setItem(`${this.taskId}-contract-hold-memberId`, response.data?.projectMemberList.length)
            }else {
              sessionStorage.setItem(`${this.taskId}-contract-memberId`, response.data?.projectMemberList.length)
            }
          } else {
            activeFormList.push(6);
          }
          sessionStorage.removeItem(`${this.taskId}-contract-budgetId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-budgetId`);
          if (response.data?.projectBudget && response.data?.projectBudget?.id) {
            if(response.data?.projectBudget.isSubmit==='1'){
              sessionStorage.setItem(`${this.taskId}-contract-budgetId`, response.data.projectBudget.id)
            }else {
              sessionStorage.setItem(`${this.taskId}-contract-hold-budgetId`, response.data.projectBudget.id)
              activeFormList.push(7);
            }
          } else {
            activeFormList.push(7);
          }
          sessionStorage.removeItem(`${this.taskId}-contract-termsId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-termsId`);
          if (response.data?.contractTerms && response.data?.contractTerms.length>0) {
            const n = response.data?.contractTerms.filter(val=>val.isSubmit === '0')
            if(n.length>0){
              activeFormList.push(8);
              sessionStorage.setItem(`${this.taskId}-contract-hold-termsId`, response.data?.contractTerms.length)
            }else {
              sessionStorage.setItem(`${this.taskId}-contract-termsId`, response.data?.contractTerms.length)
            }
          } else {
            activeFormList.push(8);
          }
          sessionStorage.removeItem(`${this.taskId}-contract-otherId`);
          sessionStorage.removeItem(`${this.taskId}-contract-hold-otherId`);
          if (response.data?.contractOthers && response.data?.contractOthers?.id) {
            if(response.data?.contractOthers.isSubmit==='1'){
              sessionStorage.setItem(`${this.taskId}-contract-otherId`, response.data.contractOthers.id)
            }else {
              sessionStorage.setItem(`${this.taskId}-contract-hold-otherId`, response.data.contractOthers.id)
              activeFormList.push(9);
            }
          } else {
            activeFormList.push(9);
          }
          if (activeFormList.length <= 0) {
            sessionStorage.setItem(`${this.taskId}-contract-activeForm`, 9); // 跳转到最后一步
          } else {
            sessionStorage.setItem(`${this.taskId}-contract-activeForm`, activeFormList[0]); // 跳转到第一个不满足
          }
          this.openRWS = true;
        }else {
          this.openRWS = true;
        }
      }).catch((e) => {
        this.$message.error(e.toString());
      });
      // this.$api.kjxm.contract.getByProject(this.taskId).then(response => {
      //   if(response.data){
      //     sessionStorage.setItem(`${this.taskId}-contractId`, response.data.id)
      //     sessionStorage.setItem(`${this.taskId}-contract-contractId`, response.data.id)
      //   }
      //   this.openRWS = true
      // }).catch((e) => {
      //   this.$message.error(e.toString());
      // });
    },
    acceptanceInit(){
      sessionStorage.removeItem(`${this.taskId}-acceptance-activeForm`);
      this.$api.kjxm.acceptance.getByProject(this.taskId).then(response => {
        if(response.data){
          sessionStorage.setItem(`${this.taskId}-acceptanceId`, response.data.id);
          sessionStorage.setItem(`${this.taskId}-acceptance-acceptanceId`, response.data.id);
        }
        this.openYS = true
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    changeTab(value){
      sessionStorage.setItem(`${this.taskId}-all-apply-active`,value)
    }
  }
}
</script>

<style scoped>

</style>
